package com.xxl.interceptor;

import com.xxl.util.FiledConstant;
import com.xxl.util.UrlConstant;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author xxl
 * @date 2023/3/8
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(FiledConstant.USER_INFO);
        //用户信息存在放行
        if (attribute != null) {
            return true;
        }
        //不存在不放行
        request.getRequestDispatcher(UrlConstant.JUMP_LOGIN_PAGE).forward(request,response);
        return false;
    }
}
